package com.idanchuang.component.businesslog.es.index;

import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.core.AcknowledgedResponse;
import org.elasticsearch.client.indexlifecycle.GetLifecyclePolicyRequest;
import org.elasticsearch.client.indexlifecycle.GetLifecyclePolicyResponse;

import java.io.IOException;

import static java.util.Collections.emptySet;

/**
 * The IndexStateManageClient
 *
 * @since 2020/7/17
 */
public class IndexManageClient {

    private final CustomRestHighLevelClient restHighLevelClient;

    IndexManageClient(CustomRestHighLevelClient restHighLevelClient) {
        this.restHighLevelClient = restHighLevelClient;
    }

    /**
     * Retrieve one or more lifecycle policy definition. See
     * <a href="https://opendistro.github.io/for-elasticsearch-docs/docs/ism/api/#get-policy">
     * the docs</a> for more.
     * @param request the request
     * @param options the request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized
     * @return the response
     * @throws IOException in case there is a problem sending the request or parsing back the response
     */
    public GetLifecyclePolicyResponse getLifecyclePolicy(GetLifecyclePolicyRequest request,
                                                         RequestOptions options) throws IOException {
        return restHighLevelClient.performRequestAndParseEntityCustom(request, IndexManageConverters::getLifecyclePolicy, options,
                GetLifecyclePolicyResponse::fromXContent, emptySet());
    }

    /**
     * Create or modify a lifecycle definition. See <a href=
     * "https://opendistro.github.io/for-elasticsearch-docs/docs/ism/api/#create-policy">
     * the docs</a> for more.
     * @param request the request
     * @param options the request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized
     * @return the response
     * @throws IOException in case there is a problem sending the request or parsing back the response
     */
    public AcknowledgedResponse putLifecyclePolicy(PutIndexManagerPolicyRequest request,
                                                   RequestOptions options) throws IOException {
        return restHighLevelClient.performRequestAndParseEntityCustom(request, IndexManageConverters::putLifecyclePolicy, options,
                AcknowledgedResponse::fromXContent, emptySet());
    }
}
